package config

import (
	"errors"
	"gopkg.in/yaml.v3"
	"os"
)

var (
	Config Model
)

type Model struct {
	Rocketmq Rocketmq `yaml:"rocketmq"`
	Logs     Logs     `yaml:"logs"`
}

func init() {
	Config = Model{
		Rocketmq: Rocketmq{
			RocketMqDelayMaxTime: 24 * 60 * 60,
		},
		Logs: Logs{
			IsOpenLogs: false,
			LogsLevel:  "info",
			LogsPath:   "./../runtime/logs/rocketmq",
			IsConsole:  false,
		},
	}
}

func (m *Model) LoadConfig(file string) error {
	configDefaultData, defaultErr := os.ReadFile(file)
	if defaultErr != nil {
		return errors.New("配置文件读取失败，错误信息：" + defaultErr.Error())
	}
	err := yaml.Unmarshal(configDefaultData, &Config)
	if err != nil {
		return errors.New("配置文件解析失败，错误信息：" + err.Error())
	}
	return nil
}
